package com.dragon.modules.yuegou.service.impl.marketing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.marketing.AnnouncementEntity;
import com.dragon.modules.yuegou.enums.marketing.AnnouncementStatus;
import com.dragon.modules.yuegou.mapper.marketing.AnnouncementMapper;
import com.dragon.modules.yuegou.model.marketing.Announcement;
import com.dragon.modules.yuegou.query.marketing.AnnouncementQuery;
import com.dragon.modules.yuegou.service.marketing.AnnouncementService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公告信息表 服务实现
 *
 * @author px
 * @date 2019-04-08 14:44:49
 * @version 2.0
 */
@Service("mktAnnouncementService")
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, AnnouncementEntity>
		implements AnnouncementService {

	@Override
	public Announcement detail(String announcementId) {
		AnnouncementEntity announcement = getById(announcementId);
		if (announcement != null && announcement.getReadVolume() != null) {
			announcement.setReadVolume(announcement.getReadVolume() + 1);
			baseMapper.updateById(announcement);
		}
		return new Announcement(announcement);
	}

	@Override
	public List<Announcement> findAnnouncementList(String busdId, PageRequest pageInfo) {
		Check.notNull(busdId, "商圈不能为空");
		AnnouncementQuery query = new AnnouncementQuery();
		query.setBusdId(busdId);
		query.setStatus(AnnouncementStatus.SEND);

		QueryWrapper<AnnouncementEntity> wrapper = query.toQueryWrapper();
		wrapper.orderByDesc(AnnouncementEntity.COL_CREATED_TIME);
		return page(PageToResultUtils.resultToPage(pageInfo), wrapper).getRecords();
	}

	@Override
	public void createAnnounce(AnnouncementQuery query){
		Check.notNull(query.getBusdId(),"商圈不能为空");
		Check.notEmpty(query.getTitle(),"标题不能为空");
		Check.notNull(query.getType(),"类型不能为空");
		Check.notNull(query.getRemarks(),"简介不能为空");
		Check.notNull(query.getInfo(),"内容不能为空");
		Check.notNull(query.getImg(),"请上传图片");
		Announcement announcement = new Announcement();
		announcement.setBusdId(query.getBusdId());
		announcement.setTitle(query.getTitle());
		announcement.setType(query.getType());
		announcement.setRemarks(query.getRemarks());
		announcement.setInfo(query.getInfo());
		announcement.setImg(query.getImg());
		if(StringUtils.isEmpty(query.getId())) {
			announcement.setStatus(AnnouncementStatus.UNSEND);
			announcement.setDel(false);
			save(announcement);
		}else{
			announcement.setId(query.getId());
			baseMapper.updateById(announcement);
		}
	}

	@Override
	public void sendAnnounce(String id){
		Check.notNull(id,"id不能为空");
		Announcement announcement = new Announcement();
		announcement.setId(id);
		announcement.setStatus(AnnouncementStatus.SEND);
		baseMapper.updateById(announcement);
	}

	@Override
	public void delAnnounce(String id){
		Check.notNull(id,"id不能为空");
		removeById(id);
	}

}